/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView.dish;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.RPJButtonCooking;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
import com.ruipengkj.component.jpanel.RepeatImagePanel;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.view.orderView.dish.cooking.CommodityExeAttr;
import com.ruipengkj.view.orderView.dish.cooking.CommodityExeAttrOption;
/**
 * 说明：
 * 烹饪方式（加料）
 */
public class OrderDishCookingDialog extends IDialog {

	private static final long serialVersionUID = 1L;

	private JButton sureButton;//确定
	private JButton cancleButton;//取消
	
	private List<RPJButtonCooking> jbList = new ArrayList<RPJButtonCooking>();
	public boolean isSuccess = false;//成功
	
	private ImageIcon icon=ImageFactory.loadImage(ImageFactory.ICON_4);
	
//	private String cooking = "";
	public OrderDishCookingDialog() {
		super((IDialog)null, "选中调料", true);
		init();
		this.setLocationRelativeTo(null);
	}
	
	private void init() {
		sureButton = new RPJButtonWhite("确 定");
		cancleButton = new RPJButtonWhite("取  消");
		sureButton.addActionListener(this);
		cancleButton.addActionListener(this);
		Color color = new Color(247, 247, 247);
		
		JPanel jp1 = new RepeatImagePanel(ImageFactory.loadImage(ImageFactory.DIALOG_DOWN_BG_1).getImage());
		jp1.add(sureButton);
		jp1.add(new JLabel("          "));
		jp1.add(cancleButton);
	
		int width = 0;//记录水平的最大组件数
		int height = 0;//记录垂直的烈数
		ButtonGroup bg = null;
		
		JPanel mainPanel = uiFactory.createBoxJPanel(BoxLayout.Y_AXIS);
//		mainPanel.setBorder(new TopBottomLineBorder(new Color(246, 163, 49), true));
		
		List<CommodityExeAttr> attrs = ApplicationContext.getCommodityExeAttrs().getAttrs();
		height = attrs.size();
		for (int i = 0; i < height; ++i) {
			
			JPanel jp = uiFactory.createFlowJPanel(FlowLayout.LEFT);
			
			CommodityExeAttr attr = attrs.get(i);
			JLabel label = new JLabel(attr.getText()+"：", icon, Label.LEFT);
			jp.add(new JLabel("     "));
			jp.add(label);
			
			if (attr.getAttrType() == CommodityExeAttr.EXE_ATTR_TYPE_SINGLE_CHOOSE) {
				bg = new ButtonGroup();
			}
			
			List<CommodityExeAttrOption> Options = attr.getOptions();
			for (int j = 0, size = Options.size(); j < size; ++j) {
				if (size > width)
					width = size;
				RPJButtonCooking tb = new RPJButtonCooking(Options.get(j).getText());
				tb.setName(Options.get(j).getValue());
				tb.setParentName(attr.getName());
				jbList.add(tb);
				if (attr.getAttrType() == CommodityExeAttr.EXE_ATTR_TYPE_SINGLE_CHOOSE) {
					bg.add(tb);
				}
				jp.add(tb);
			}
			mainPanel.add(jp);
		}
		
		Util.setSize(this, width * 123+100, height * 63+80+50);
		
		JPanel jp = new BorderJPanel(new BorderLayout());
		jp.add(mainPanel);
		jp.add(jp1, BorderLayout.SOUTH);
		jp.setOpaque(true);
		jp.setBackground(color);
		jp.setCursor(Util.CURSOR_DEFAULT);
		
		this.add(jp);
	}
	
	public void showDialog(String cooking) {
		if (cooking != null && !cooking.equals("")) {
			//spicy:spicy0;Garlic:spicy1,spicy2 --> spicy:spicy0 和 Garlic:spicy1,spicy2
			String[] attrs = cooking.split(";");
			for (int i = attrs.length - 1; i >= 0; --i) {
				//Garlic:spicy1,spicy2 --> Garlic 和  spicy1,spicy2
				String[] string = attrs[i].split(":");
				String attr = string[0];
				
				//spicy1,spicy2 --> spicy1 和 spicy2
				String[] Options = string[1].split(",");
				for (int j = Options.length-1; j >= 0; --j) {
					//遍历所有按钮，选中按钮  attr相同  Option相同
					for (int t = jbList.size() - 1; t >= 0; --t) {
						System.out.println(jbList.get(t).getName() + "  "+Options[j]);
						RPJButtonCooking jb = jbList.get(t);
						if (jb.getParentName().equals(attr) && jb.getName().equals(Options[j])) {
							jb.setSelected(true);
							break;
						}
					}
				}
			}
		}
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == sureButton) {
			isSuccess = true;
			this.dispose();
		}
		this.dispose();
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			isSuccess = true;
			this.dispose();
		} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			this.dispose();
		}
	
	}
	
	public String getText() {
		//spicy:spicy0;Garlic:spicy1,spicy2 --> 最后的样式
		String cooking = "";
		
		RPJButtonCooking jb = null;
		for (int i = jbList.size() - 1; i >= 0; --i) {
			if (jbList.get(i).isSelected()) {
				if (jb != null) {
					if (jb.getParentName().equals(jbList.get(i).getParentName())) {
						jb = jbList.get(i);
						//Garlic:spicy1 加 spicy2 --> Garlic:spicy1,spicy2
						cooking += "," + jb.getName();
						continue;
					} else {
						jb = jbList.get(i);
						//spicy:spicy0  加  Garlic 加 spicy1 --> spicy:spicy0;Garlic:spicy1
						cooking += ";" + jb.getParentName()+ ":" +jb.getName();
					}
				} else {
					jb = jbList.get(i);
					//cooking为""时，spicy 加 spicy0 --> spicy:spicy0
					cooking += jb.getParentName()+ ":" +jb.getName();
				}
				
			}
		}
		return cooking;
	}
	
	public static void main(String[] args) {
		OrderDishCookingDialog dialog = new OrderDishCookingDialog();
		dialog.setVisible(true);
	}
}
